package com.imessage.action.business.apple;

import com.bean.BusinessApple;
import com.bean.BusinessWuma;
import com.imessage.action.BaseAction;
import com.imessage.type.BusinessAppleState;
import com.imessage.type.BusinessWumaState;
import com.imessage.utils.DateUtil;
import com.imessage.utils.StrUtil;
import com.imessage.vo.AppleVo;
import com.imessage.vo.WumaVo;
import io.ebean.Expr;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
public class AppleBaseAction extends BaseAction {

    protected final String datagrid = "datagrid-businessApple-filter";

    protected void setExpressionList(List<Expression> expressionList, AppleVo appleVo) {
        if (StringUtils.isNotBlank(appleVo.getAppleId())) {
            expressionList.add(Expr.contains("appleId", appleVo.getAppleId()));
        }
        if (StringUtils.isNotBlank(appleVo.getState())) {
            expressionList.add(Expr.eq("state", appleVo.getState()));
        }
    }


    protected void setModelAndView(ModelAndView modelAndView, BusinessApple businessApple) {
        modelAndView.addObject("businessAppleForm", businessApple);
        modelAndView.addObject("statusList", BusinessAppleState.values());
    }

    protected void setApple(AppleVo appleVo, BusinessApple businessApple) {
        copyProperties(appleVo, businessApple);
        if (StringUtils.isNotBlank(appleVo.getState())) {
            businessApple.setState(BusinessAppleState.fromValue(appleVo.getState()));
        }
    }

    protected String checkData(AppleVo appleVo, BusinessApple businessApple) {
        BusinessApple apple=baseService.findObject(
                BusinessApple.class, Expr.eq("appleId", appleVo.getAppleId()));
        if(apple==null){
            return "";
        }
        if(businessApple==null){
            return "appleId已存在";
        }
        if(apple.getAppleId().equalsIgnoreCase(businessApple.getAppleId().toLowerCase())){
            return "";
        }
        return "appleId已存在";
    }


    protected boolean saveExcel(String filePath) {
        try {
            File file = new File(filePath);
            InputStream is = new FileInputStream(file);
            String fileName = file.getName();
            Workbook hssfWorkbook = null;
            if (fileName.endsWith("xlsx")) {
                hssfWorkbook = new XSSFWorkbook(is);    //Excel 2007
            } else if (fileName.endsWith("xls")) {
                hssfWorkbook = new HSSFWorkbook(is);    //Excel 2003
            }
            // 循环工作表Sheet
            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
                Sheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
                if (hssfSheet == null) {
                    continue;
                }
                List<String> headlist = new ArrayList<String>();
                Row row0 = hssfSheet.getRow(0);// 第一张sheet的第一行
                if (row0 == null) {
                    continue;
                }
                for (int cellNum = 0; cellNum < row0.getLastCellNum(); cellNum++) {
                    Cell cell = row0.getCell(cellNum);
                    headlist.add(cell.getStringCellValue().trim());// 读取
                }
                // 循环行Row
                for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                    Row row = hssfSheet.getRow(rowNum);
                    if (row == null) {
                        continue;
                    }
                    BusinessApple businessApple = new BusinessApple();
                    for (int j = 0; j < headlist.size(); j++) { // 对每个单元格进行循环
                        String value = headlist.get(j);// 读取当前单元格的值
                        Cell cell = row.getCell(j);
                        if(cell!=null){
                            cell.setCellType(CellType.STRING);
                        }else{
                            continue;
                        }
                        if (StringUtils.isBlank(cell.getStringCellValue())) {
                            continue;
                        }
                        String content = cell.getStringCellValue().trim();
                        if (value.toLowerCase().indexOf("appleid") != -1) {
                            businessApple.setAppleId(content);
                        }
                        if (value.toLowerCase().indexOf("password") != -1) {
                            businessApple.setApplePasswd(content);
                        }
                        if (value.toLowerCase().indexOf("remark") != -1) {
                            businessApple.setRemark(content);
                        }
                    }
                    if(StringUtils.isBlank(businessApple.getApplePasswd())){
                        continue;
                    }
                    BusinessApple apple = baseService.findObject(BusinessApple.class, Expr.eq("appleId", businessApple.getAppleId()));
                    if (apple == null) {
                        businessApple.setState(BusinessAppleState.available);
                        baseService.saveObject(businessApple);
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
            return false;
        }
        return true;
    }
}
